home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1999 / MacHack 1999.toast / The Hacks / MACtive Desktop / Source / Sources / BaseDialog.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-14  |  6.3 KB  |  387 lines  |  [TEXT/CWIE]

  1. #include <stdarg.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "QDContext.h"
  6. #include "WindowManager.h"
  7. #include "BaseDialog.h"
  8.  
  9.  
  10.  
  11.  
  12.  
  13. extern WindowManager                        *gWindowManager;
  14.  
  15.  
  16. enum
  17. {
  18.     kOKButtonID                = 1,
  19.     kCancelButtonID            = 2
  20. };
  21.  
  22.  
  23.  
  24.  
  25.  
  26. BaseDialog::BaseDialog(UInt32 dialogID,Boolean isModal)
  27. {
  28.     fDialog = ::GetNewDialog(dialogID,NULL,(WindowPtr)-1L);
  29.     if (fDialog != NULL)
  30.     {
  31.         fFlags = isModal ? (kDialog | kModal) : kDialog;
  32.         fWindow = (WindowPtr)fDialog;
  33.         gWindowManager->DoAddWindow(this);
  34.         SelectWindow(fWindow);
  35.     }
  36. }
  37.  
  38.  
  39.  
  40.  
  41.  
  42. BaseDialog::~BaseDialog(void)
  43. {
  44.     if (fDialog != NULL)
  45.     {
  46.         gWindowManager->DoDeleteWindow(this);
  47.         DisposeDialog(fDialog);
  48.         fDialog = NULL;
  49.         fWindow = NULL;
  50.     }
  51. }
  52.  
  53.  
  54.  
  55.  
  56.  
  57. void BaseDialog::DoDialogEvent(EventRecord *event)
  58. {
  59.     QDContext    context(fWindow);
  60.     
  61.     
  62.     // We don't do any special preprocessing of
  63.     // events (yet) so just pass it straight thru
  64.     HandleDialogEvent(event);
  65. }
  66.  
  67.  
  68. #if 0
  69. #pragma mark -
  70. #endif
  71.  
  72.  
  73. void BaseDialog::HandleDialogEvent(EventRecord *event)
  74. {
  75.     DialogPtr    dialog;
  76.     short        item;
  77.     
  78.     
  79.     if (HandleDialogEventFilter(event))
  80.     {
  81.         if (DialogSelect(event,&dialog,&item))
  82.             HandleDialogItemhit(item);
  83.         
  84.         // We have to check here to see if we've been deleted
  85.         // because HandleDialogItemhit might have been a hit to
  86.         // the OK or Cancel button.
  87.         if (fDialog != NULL)
  88.         {
  89.             // Automagically handle the hiliting
  90.             // and updating of the OK button
  91.             switch(event->what)
  92.             {
  93.                 case keyDown:
  94.                 case keyUp:
  95.                 case mouseDown:
  96.                 case mouseUp:
  97.                     // Only query the OK button state if something
  98.                     // happened that could have possibly changed it
  99.                     SetOKState(HandleOKButtonHiliteQuery());
  100.                     break;
  101.                 
  102.                 case updateEvt:
  103.                     DrawThickOutline(kOKButtonID);
  104.                     break;
  105.             }
  106.         }
  107.     }
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. Boolean BaseDialog::HandleDialogEventFilter(EventRecord *event)
  115. {
  116.     ControlHandle    control;
  117.     short            type;
  118.     Rect            box;
  119.     
  120.     
  121.     // Filter out command/option/control keys
  122.     if (event->what == keyDown && (event->modifiers & (cmdKey | optionKey | controlKey)))
  123.         return false;
  124.     
  125.     switch(event->what)
  126.     {
  127.         case keyDown:
  128.         case autoKey:
  129.             switch(charCodeMask & event->message)
  130.             {
  131.                 case 0x0D:    // RETURN
  132.                     GetDialogItem(fDialog,kOKButtonID,&type,(Handle*)&control,&box);
  133.                     if ((type == 4) && (control[0]->contrlHilite == 0))
  134.                     {
  135.                         AnimateButtonPress(kOKButtonID);
  136.                         HandleDialogItemhit(kOKButtonID);
  137.                         return false;
  138.                     }
  139.                     break;
  140.                 
  141.                 case 0x1B:    // ESC
  142.                     GetDialogItem(fDialog,kCancelButtonID,&type,(Handle*)&control,&box);
  143.                     if ((type == 4) && (control[0]->contrlHilite == 0))
  144.                     {
  145.                         AnimateButtonPress(kCancelButtonID);
  146.                         HandleDialogItemhit(kCancelButtonID);
  147.                         return false;
  148.                     }
  149.                     break;
  150.             }
  151.             break;
  152.     }
  153.     
  154.     // Continue processing event
  155.     return true;
  156. }
  157.  
  158.  
  159.  
  160.  
  161.  
  162. void BaseDialog::HandleDialogItemhit(short item)
  163. {
  164.     // Default behavior for OK and Cancel
  165.     // button hits are to dismiss the dialog
  166.     switch(item)
  167.     {
  168.         case kOKButtonID:
  169.         case kCancelButtonID:
  170.             delete this;
  171.             break;
  172.     }
  173. }
  174.  
  175.  
  176.  
  177.  
  178.  
  179. Boolean BaseDialog::HandleOKButtonHiliteQuery(void)
  180. {
  181.     // Default behavior is to have a
  182.     // hilited and active OK button
  183.     return true;
  184. }
  185.  
  186.  
  187. #if 0
  188. #pragma mark -
  189. #endif
  190.  
  191.  
  192. void BaseDialog::DrawThickOutline(short item)
  193. {
  194.     QDContext        context(fWindow);
  195.     ControlHandle    control;
  196.     short            type;
  197.     Rect            box;
  198.     
  199.     
  200.     // Draw a thick border around an item (default button outline)
  201.     GetDialogItem(fDialog,item,&type,(Handle*)&control,&box);
  202.     if (type == 4)
  203.     {
  204.         InsetRect(&box,-4,-4);
  205.         
  206.         if (control[0]->contrlHilite == 255)
  207.             PenPat(&qd.gray);
  208.         
  209.         PenSize(3,3);
  210.         FrameRoundRect(&box,16,16);
  211.     }
  212. }
  213.  
  214.  
  215.  
  216.  
  217.  
  218. void BaseDialog::SetOKState(Boolean isActive)
  219. {
  220.     ControlHandle    control;
  221.     short            type;
  222.     Rect            box;
  223.     
  224.     
  225.     // (De)Hilite the OK button to show its (in)active state
  226.     GetDialogItem(fDialog,kOKButtonID,&type,(Handle*)&control,&box);
  227.     if (type == 4)
  228.     {
  229.         if (isActive)
  230.         {
  231.             if (control[0]->contrlHilite == 255)
  232.             {
  233.                 HiliteControl(control,0);
  234.                 DrawThickOutline(kOKButtonID);
  235.             }
  236.         }
  237.         else
  238.         {
  239.             if (control[0]->contrlHilite != 255)
  240.             {
  241.                 HiliteControl(control,255);
  242.                 DrawThickOutline(kOKButtonID);
  243.             }
  244.         }
  245.     }
  246. }
  247.  
  248.  
  249.  
  250.  
  251.  
  252. void BaseDialog::AnimateButtonPress(short item)
  253. {
  254.     ControlHandle    control;
  255.     UInt32            ticks;
  256.     short            type;
  257.     Rect            box;
  258.     
  259.     
  260.     // Make it appear to the user that a button was pressed
  261.     GetDialogItem(fDialog,item,&type,(Handle*)&control,&box);
  262.     if ((type == 4) && (control[0]->contrlHilite == 0))
  263.     {
  264.         HiliteControl(control,kControlButtonPart);
  265.         Delay(10,&ticks);
  266.         HiliteControl(control,0);
  267.     }
  268. }
  269.  
  270.  
  271.  
  272.  
  273.  
  274. void BaseDialog::GetItemText(short item,char *text)
  275. {
  276.     Handle    data;
  277.     short    type;
  278.     Rect    rect;
  279.     Str255    pstr;
  280.     
  281.     
  282.     // Get the text of a static text item or an
  283.     // edit text item and return it as a c string
  284.     GetDialogItem(fDialog,item,&type,&data,&rect);
  285.     if ((type == statText) || (type == editText))
  286.         GetDialogItemText(data,pstr);
  287.     else
  288.         pstr[0] = '\0';
  289.     
  290.     pstr[1 + pstr[0]] = '\0';
  291.     strcpy(text,(char*)&pstr[1]);
  292. }
  293.  
  294.  
  295.  
  296.  
  297.  
  298. int BaseDialog::GetItemTextAsDecimal(short item)
  299. {
  300.     char    text[256];
  301.     
  302.     
  303.     // Get the text of a static text item or an edit
  304.     // text item and return it as a decimal value
  305.     GetItemText(item,text);
  306.     return atoi(text);
  307. }
  308.  
  309.  
  310.  
  311.  
  312.  
  313. void BaseDialog::SetItemText(short item,char *text)
  314. {
  315.     Handle    data;
  316.     short    type;
  317.     Rect    rect;
  318.     Str255    pstr;
  319.     
  320.     
  321.     // Set the text of a static text item
  322.     // or an edit text item from a c string
  323.     GetDialogItem(fDialog,item,&type,&data,&rect);
  324.     if ((type == statText) || (type == editText))
  325.     {
  326.         pstr[0] = strlen(text);
  327.         strcpy((char*)&pstr[1],text);
  328.         SetDialogItemText(data,pstr);
  329.     }
  330. }
  331.  
  332.  
  333.  
  334.  
  335.  
  336. void BaseDialog::SetItemTextf(short item,char *format,...)
  337. {
  338.     va_list    args;
  339.     char    text[256];
  340.     
  341.     
  342.     // Set the text of a static text item or an edit
  343.     // text item from a printf formatted specification
  344.     va_start(args,format);
  345.     vsprintf(text,format,args);
  346.     va_end(args);
  347.     
  348.     SetItemText(item,text);
  349. }
  350.  
  351.  
  352.  
  353.  
  354.  
  355. void BaseDialog::AppendItemText(short item,char *text)
  356. {
  357.     char    cur[256];
  358.     
  359.     
  360.     // Append text to the end of a static text
  361.     // item or an edit text item from a c string
  362.     GetItemText(item,cur);
  363.     strcat(cur,text);
  364.     SetItemText(item,cur);
  365. }
  366.  
  367.  
  368.  
  369.  
  370.  
  371. void BaseDialog::AppendItemTextf(short item,char *format,...)
  372. {
  373.     va_list    args;
  374.     char    cur[256],text[256];
  375.     
  376.     
  377.     // Append text to the end of a static text item or an
  378.     // edit text item from a printf formatted specification
  379.     va_start(args,format);
  380.     vsprintf(text,format,args);
  381.     va_end(args);
  382.     
  383.     GetItemText(item,cur);
  384.     strcat(cur,text);
  385.     SetItemText(item,cur);
  386. }
  387.